package com.gree.changying.base;

import java.lang.ref.WeakReference;

//presenter 基类
public abstract  class BasePresenter<V extends BaseView,M extends BaseModel,CONTRACT> {
    protected M m;
    //绑定view 层的弱引用 一旦调用destory  能够很好的切断连接 ，解除绑定，防止内存泄漏
    private WeakReference<V> vWeakReference;

    public BasePresenter(){
        m = getModel();
    }

    //获取子类具体锲约（Model层和View层协商的共同业务）
    public abstract CONTRACT getContract();

    public  void bindView(V v) {
        vWeakReference = new WeakReference<>(v);
    }
    //获取View P ---> V的过程
    public V getView(){
        if(vWeakReference!=null){
            return vWeakReference.get();
        }
        return null;
    }
    public void unBindView() {
        if(vWeakReference!=null){
            //清空弱引用  回收对象
            vWeakReference.clear();
            vWeakReference = null;
            System.gc();
        }
    }
    public abstract M getModel();
}
